home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 SRC / Mac / Demo / resources / copyres.py next >
Text File  |  1995-01-30  |  1KB  |  58 lines

  1. from Res import *
  2. from Resources import *
  3. import MacOS
  4.  
  5. READ = 1
  6. WRITE = 2
  7. smAllScripts = -3
  8.  
  9. def copyres(src, dst):
  10.     """Copy resource from src file to dst file."""
  11.     
  12.     cur = CurResFile()
  13.     ctor, type = MacOS.GetCreatorAndType(src)
  14.     input = FSpOpenResFile(src, READ)
  15.     try:
  16.         FSpCreateResFile(dst, ctor, type, smAllScripts)
  17.     except:
  18.         raw_input("%s already exists...  CR to write anyway! " % dst)
  19.     output = FSpOpenResFile(dst, WRITE)
  20.     UseResFile(input)
  21.     ntypes = Count1Types()
  22.     for itype in range(1, 1+ntypes):
  23.         type = Get1IndType(itype)
  24.         nresources = Count1Resources(type)
  25.         for ires in range(1, 1+nresources):
  26.             res = Get1IndResource(type, ires)
  27.             res.LoadResource()
  28.             id, type, name = res.GetResInfo()
  29.             size = res.SizeResource()
  30.             attrs = res.GetResAttrs()
  31.             print id, type, name, size, hex(attrs)
  32.             res.DetachResource()
  33.             UseResFile(output)
  34.             try:
  35.                 res2 = Get1Resource(type, id)
  36.             except (RuntimeError, Res.Error), msg:
  37.                 res2 = None
  38.             if res2:
  39.                 print "Duplicate type+id, not copied"
  40.                 print (res2.size, res2.data)
  41.                 print res2.GetResInfo()
  42.                 if res2.HomeResFile() == output:
  43.                     'OK'
  44.                 elif res2.HomeResFile() == input:
  45.                     'BAD!'
  46.                 else:
  47.                     print 'Home:', res2.HomeResFile()
  48.             else:
  49.                 res.AddResource(type, id, name)
  50.                 #res.SetResAttrs(attrs)
  51.                 res.WriteResource()
  52.             UseResFile(input)
  53.     UseResFile(cur)
  54.     CloseResFile(output)
  55.     CloseResFile(input)
  56.  
  57. copyres('::python.π.rsrc', '::foo.rsrc')
  58.